package coe.pumbaa

import java.text.StringCharacterIterator
import java.text.CharacterIterator

class StringManagerService {

    def stringFilter(def text){
        return text
    }
    
    def checkObjectInString(def object,def text){
        def check = false
        if(text.indexOf((String)object.id) > -1){
            check = true
        }
        return check
    }
    
    def countMemberInString(def text){
        if(text.size() == 0){
            return 0
        }
        def count = text.split(",")
        return count.size()
    }
    
    def getMemberListInString(def text){
        if(text.size() == 0){
            return []
        }
        return text.split(",")
    }
    
    def deleteObjectFromString(def object,def text){
        if(text.indexOf((String)object.id) > -1){
            def textList = text.split(",")
            text = ""
            textList.each{
                if(!it.equals((String)object.id)){
                    if(text.equals("")){
                        text = it
                    }else{
                        text = text + "," + it
                    }
                }
            }
        }
        return text
    }
    
    def addObjectInToString(def object,def text){
        if(text.indexOf((String)object.id) < 0){
            if(text.size() == 0){
                text = (String)object.id
            }else{
                text = text+","+(String)object.id
            }
        }
        return text
    }
    
    def stringToHTML(String aText){
        if(aText == null){
            return ""
        }
        final StringBuilder result = new StringBuilder()
        final StringCharacterIterator iterator = new StringCharacterIterator(aText)
        char character =  iterator.current()
        while (character != CharacterIterator.DONE ){
            if (character == '<') {
                result.append("&lt;")
            }
            else if (character == '>') {
                result.append("&gt;")
            }
            else if (character == '\n') {
                result.append("<br>")
            }
            else if (character == '&') {
                result.append("&amp;")
            }
            else if (character == '\"') {
                result.append("&quot;")
            }
            else if (character == '\t') {
                addCharEntity(9, result)
            }
            else if (character == '!') {
                addCharEntity(33, result)
            }
            else if (character == '#') {
                addCharEntity(35, result)
            }
            else if (character == '$') {
                addCharEntity(36, result)
            }
            else if (character == '%') {
                addCharEntity(37, result)
            }
            else if (character == '\'') {
                addCharEntity(39, result)
            }
            else if (character == '(') {
                addCharEntity(40, result)
            }
            else if (character == ')') {
                addCharEntity(41, result)
            }
            else if (character == '*') {
                addCharEntity(42, result)
            }
            else if (character == '+') {
                addCharEntity(43, result)
            }
            else if (character == ',') {
                addCharEntity(44, result)
            }
            else if (character == '-') {
                addCharEntity(45, result)
            }
            else if (character == '.') {
                addCharEntity(46, result)
            }
            else if (character == '/') {
                addCharEntity(47, result)
            }
            else if (character == ':') {
                addCharEntity(58, result)
            }
            else if (character == ';') {
                addCharEntity(59, result)
            }
            else if (character == '=') {
                addCharEntity(61, result)
            }
            else if (character == '?') {
                addCharEntity(63, result)
            }
            else if (character == '@') {
                addCharEntity(64, result)
            }
            else if (character == '[') {
                addCharEntity(91, result)
            }
            else if (character == '\\') {
                addCharEntity(92, result)
            }
            else if (character == ']') {
                addCharEntity(93, result)
            }
            else if (character == '^') {
                addCharEntity(94, result)
            }
            else if (character == '_') {
                addCharEntity(95, result)
            }
            else if (character == '`') {
                addCharEntity(96, result)
            }
            else if (character == '{') {
                addCharEntity(123, result)
            }
            else if (character == '|') {
                addCharEntity(124, result)
            }
            else if (character == '}') {
                addCharEntity(125, result)
            }
            else if (character == '~') {
                addCharEntity(126, result)
            }
            else {
                //the char is not a special one
                //add it to the result as is
                result.append(character)
            }
            character = iterator.next()
        }
        return findImgTag(result.toString())
    }
    
    private String findImgTag(def text){
        text = text.replace("&#091;img&#093;","<img src=\"")
        text = text.replace("&#091;&#047;img&#093;","\"/>")
        return text
    }
    
    private void addCharEntity(Integer aIdx, StringBuilder aBuilder){
        String padding = "";
        if( aIdx <= 9 ){
            padding = "00";
        }
        else if( aIdx <= 99 ){
            padding = "0";
        }
        else {
            //no prefix
        }
        String number = padding + aIdx.toString();
        aBuilder.append("&#" + number + ";");
    }
}
